package filter

import (
	"adam2/internal/properties"
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 过滤器：市盈率最少的(10*账号数量+4)只股票，这个需要最后一个执行
type TenLowestPeFilter struct {
	db *gorm.DB
}

// 初始化
func (t *TenLowestPeFilter) Init(db *gorm.DB) {
	t.db = db
}

// 执行
func (t *TenLowestPeFilter) DoFilter(transactionDate string) {
	io.Infoln("过滤器：市盈率最少的(10*账号数量+4)只股票，这个需要最后一个执行，交易日期[%s]", transactionDate)

	// 只保留市盈率最小的十只股票
	t.db.Exec("delete quant_stock_filter t2 where t2.stock_code not in("+
		"select t1.stock_code from (select * from quant_stock_filter t "+
		"order by t.pe asc) t1 "+
		"where rownum<=?*("+
		"select count(*) from quant_account)+4)", properties.QuantProperties_.MaxHoldStockNumber)
}
